"use client";

import type { DictData } from "@/lib/api/system/dict.d";
import { deleteDictDataApi, getDictDataListApi } from "@/lib/api/system/dict";
import {
  ActionType,
  PageContainer,
  ProColumns,
  ProTable,
  ProTableProps,
} from "@ant-design/pro-components";
import { Button, message, Popconfirm, Space } from "antd";
import { useRouter, useSearchParams } from "next/navigation";
import { useRequest } from "ahooks";
import { useRef } from "react";

export default function Dict() {
  const router = useRouter();
  const actionRef = useRef<ActionType>();
  const searchParams = useSearchParams();
  const dictType = searchParams.get("dictType");

  const { run: deleteDictData } = useRequest(deleteDictDataApi, {
    manual: true,
    onSuccess: () => {
      message.success("删除成功");
      actionRef.current?.reload();
    },
    onError: () => {
      message.error("删除失败");
    },
  });

  const columns: ProColumns<DictData>[] = [
    {
      title: "字典标签",
      dataIndex: "dictLabel",
      key: "dictLabel",
    },
    {
      title: "字典键值",
      dataIndex: "dictValue",
      key: "dictValue",
      search: false,
    },
    {
      title: "字典排序",
      dataIndex: "dictSort",
      key: "dictSort",
      search: false,
    },
    {
      title: "备注",
      dataIndex: "remark",
      search: false,
      key: "remark",
    },
    {
      title: "创建时间",
      dataIndex: "createTime",
      key: "createTime",
      valueType: "dateTime",
      width: 150,
      hideInSearch: true,
    },
    {
      title: "操作",
      key: "action",
      search: false,
      width: 100,
      render: (_, record) => (
        <Space>
          <Button type="link" onClick={() => editHandler(record.dictCode)}>
            修改
          </Button>
          <Popconfirm
            title="确定删除吗？"
            description="删除后将无法恢复！"
            onConfirm={() => {
              deleteDictData(record.dictCode);
            }}
          >
            <Button type="link" danger>
              删除
            </Button>
          </Popconfirm>
        </Space>
      ),
    },
  ];

  const proTableProps: ProTableProps<DictData, any> = {
    columns: columns,
    toolbar: {
      title: dictType,
    },
    rowKey: "dictId",
    pagination: {
      pageSize: 10,
    },
    actionRef: actionRef,
    options: {
      fullScreen: true,
    },
    toolBarRender: () => {
      return [
        <Button onClick={addHandler} key="add" type="primary">
          新增
        </Button>,
      ];
    },
    request: async (params) => {
      const res = await getDictDataListApi({ ...params, dictType: dictType });

      return {
        data: res.rows,
        success: true,
        total: res.total,
      };
    },
    onRequestError(e) {
      message.error(e.message);
    },
  };

  const addHandler = () => {
    router.push(`data/add?dictType=${dictType}`);
  };

  const editHandler = (dictId: number) => {
    router.push(`data/${dictId}?dictType=${dictType}`);
  };

  return (
    <PageContainer
      header={{
        title: false,
      }}
    >
      <ProTable {...proTableProps}></ProTable>
    </PageContainer>
  );
}
